# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import mysql.connector

# 每拿到一个Item就会调用管道
# 在settings.py ITEM_PIPELINES中启用
class ItcastPipeline(object):

    def __init__(self):
        # 可选实现，做参数初始化
        pass

    def process_item(self, item, spider):
        # item(Item对象) - 被爬取的item
        # spider(Spider对象）- 爬取item的Spider
        # 这个方法必须实现，每个item pipeline组件都需要调用该方法
        # 必须返回一个Item对象，被丢弃的item将不会再被之后的pipeline组件处理
        with open("E:\itcast.json", "a") as f:
            f.write(item['name'] + '\t' + item['title'] + '\t' + item['info'] + '\n')

        return item

    def open_spider(self, spider):
        # spider(Spider 对象) 被开启的spider
        # 可选实现，当spider被开启时，这个方法调用
        pass

    def close_spider(self, spider):
        # 可选实现，当spider被关闭时，这个方法调用
        pass


class QuotePipeline(object):

    def __init__(self):
        db_args = dict(
            host="localhost",
            db="quotes",
            user="root",
            password="root"
        )
        self.conn = mysql.connector.connect(**db_args)
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        self.insert_into_quotes(item)
        self.conn.commit()
        pass

    def insert_into_quotes(self, item):
        self.cursor.execute(
            '''
          insert into quote (quote_text, quote_author, quote_tags) 
          values (%s, %s, %s)
          ''',
            [item["text"], item["author"], item['tags']]
        )

    def close_spider(self, spider):
        self.conn.close()
        self.cursor.close()